<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/3
 * Time: 下午7:30
 */

namespace Admin\Controller;


use Common\Controller\AdminBaseController;

class PosterController extends AdminBaseController
{
    public function index(){
        $spaceid = I('get.id',0);
        $list = M('poster')->Alias('p')->field('p.id,p.name,p.disabled,p.hits,p.addtime,p.listorder,s.name spacename')->join('left join dx_posterspace s on p.spaceid = s.id')->where(['spaceid' => $spaceid])->order('listorder')->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function add(){
        if(IS_GET){
            $spaceid = I('get.id/d','');
            if(empty($spaceid)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $list = M('posterspace')->field('name')->where(['id' => $spaceid])->find();
            $this->assign('list',$list);
            $this->assign('spaceid',$spaceid);
            $this->display();
        }
    }

    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $find = M('poster')->where(['id' => $id])->find();
            if(!$find){
                $this->ajaxReturn(['status' => 1,'msg' => '记录不存在']);
            }
            $list = M('poster')->Alias('p')->field('p.id,p.name,p.startdate,p.disabled,p.setting,p.spaceid,p.enddate,p.listorder,s.name spacename')->join('left join dx_posterspace s on p.spaceid = s.id')->where(['p.id' => $id])->order('listorder')->find();

        }
        $this->assign('list',$list);
        $this->display();
    }

    public function save(){
        if(IS_POST){
            $title = I('post.title/s','');
            if(empty($title)){
                $this->ajaxReturn(['status' => 1,'msg' => '广告标题不能为空']);
            }
            $id = I('post.id/d',0);
            $disabled = I('post.status/d',0);
            $startdate = strtotime(I('post.startdate/s',0));
            $enddate = strtotime(I('post.enddate/s',0));
            $setting = I('post.setting/s','');
            $listorder = I('post.listorder/d',0);
            $spaceid = I('post.spaceid/d',0);
            if(empty($spaceid)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $poster = M('poster');
            $num = $poster->where(['name' => $title])->count();
            if(empty($id)){
                if($num > 0){
                    $this->ajaxReturn(['status' => 1,'msg' => '广告标题不能重复']);
                }
            }
            $data = [
                'id' => $id,
                'name' => $title,
                'startdate' => $startdate,
                'enddate' => $enddate,
                'addtime' => time(),
                'setting' => $setting,
                'spaceid' => $spaceid,
                'listorder' => $listorder,
                'disabled' => $disabled
            ];
            $add = $poster->add($data,[],true);
            if(!$add){
                $this->ajaxReturn(['status' => 1,'msg' => '添加失败']);
            }
            $this->ajaxReturn(['status' => 0,'msg' => '添加成功']);
        }
    }

    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $del = M('poster')->where(['id' => $id])->delete();
            if(!$del){
                $this->ajaxReturn(['status' => 1,'msg' => '删除失败']);
            }
            $this->ajaxReturn(['status' => 0,'msg' => '删除成功']);
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }
    public function changeMode(){
        if(IS_POST){
            $id = I('post.id/d','');
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $query = M('poster')->field('disabled')->where(['id' => $id])->find();
            if(empty($query['disabled'])){
                M('poster')->where(['id' => $id])->setField('disabled','1');
                $this->ajaxReturn(['status' => 0,'msg' => '已开启','data' => 1]);
            }else{
                M('poster')->where(['id' => $id])->setField('disabled','0');
                $this->ajaxReturn(['status' => 0,'msg' => '已关闭','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }
}